package com.aopcinemas.cinema.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;

import com.aopcinemas.cinema.CinemaAgent;
import com.aopcinemas.cinema.CinemaUtils;
import com.aopcinemas.cinema.event.ScheduleListListener;
import com.aopcinemas.common.ui.ScheduleTable;

public class CinemaFrame extends JFrame implements ScheduleListListener {

    private static final long serialVersionUID = 1L;

    private CinemaAgent agent;
    private CinemaRegistrationDialog cinemaRegistrationDialog;
    private JScrollPane scheduleTableScrollPane;
    private ScheduleTable scheduleTable;
    private ScheduleCreationDialog scheduleCreationDialog;

    public CinemaFrame(CinemaAgent agent) {
        this.agent = agent;
        initializeUIComponents();
        initializeFrame();
    }

    private void initializeFrame() {
        setTitle("Cinema");
        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    private void initializeUIComponents() {
        cinemaRegistrationDialog = new CinemaRegistrationDialog(this, this.agent);
        scheduleTable = new ScheduleTable();
        scheduleTableScrollPane = new JScrollPane(scheduleTable);
        scheduleCreationDialog = new ScheduleCreationDialog(this, this.agent.getScheduleDao(), this.agent.getMovieDao(), this.agent.getHallDao());
        scheduleCreationDialog.addScheduleListListener(this);
        initializeSchedulePanel();
        initializeMenuBar();
    }

    public CinemaAgent getAgent() {
        return agent;
    }

    public void setAgent(CinemaAgent agent) {
        this.agent = agent;
    }

    private void initializeSchedulePanel() {
        JPopupMenu popup = new JPopupMenu();
        JMenuItem addItem = new JMenuItem("Add");
        addItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                scheduleCreationDialog.setLocationRelativeTo(CinemaFrame.this);
                scheduleCreationDialog.setVisible(true);
            }
        });
        popup.add(addItem);
        scheduleTable.setComponentPopupMenu(popup);
        scheduleTableScrollPane.setComponentPopupMenu(popup);
        getContentPane().add(scheduleTableScrollPane);
        listUpdated();
    }

    private void initializeMenuBar() {
        JMenuBar menubar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenuItem register = new JMenuItem("Register cinema");
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                cinemaRegistrationDialog.setLocationRelativeTo(CinemaFrame.this);
                cinemaRegistrationDialog.setVisible(true);
            }
        });
        file.add(register);

        JMenuItem exit = new JMenuItem("Exit");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                CinemaFrame.this.setVisible(false);
                CinemaUtils.unregisterCinema(CinemaFrame.this.agent);
                CinemaFrame.this.agent.doDelete();
            }
        });
        file.add(exit);

        menubar.add(file);
        setJMenuBar(menubar);
    }

    @Override
    public void listUpdated() {
        scheduleTable.setSchedules(agent.getScheduleDao().getShedules());
        scheduleTable.revalidate();
        scheduleTableScrollPane.revalidate();
        scheduleTableScrollPane.repaint();
        scheduleTable.repaint();
    }
}
